package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;

/**
 * ·@author：冯。
 * ·@version：v.1.0。
 * ·@docroot：表示产生文档的根路径。
 * ·@param：方法的参数类型。
 * ·@return：方法的返回类型。
 */
public class Mob extends AbstractMapleStoryObject {
    public static Image[] imgs = new Image[100];
    static{
        for (int i = 0; i < 6; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_stand" + i);
        }
        for (int i = 6; i < 13; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_walk" + (i - 6));
        }
        for (int i = 13; i < 19; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_die" + (i - 13));
        }
        //imgs[20] = ImageUtil.getImage("blood");
        for (int i = 19; i < 25; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_stand" + (i - 19));
        }
        for (int i = 25; i < 32; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_walk" + (i - 25));
        }
        for (int i = 32; i < 38; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_die" + (i - 32));
        }
    }

    /**
     * 最大血量
     */
    public int HP_MIX;
    public Mob(){
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
        //this.dir = Direction.RIGHT;
        this.action = Action.WALK;
        this.live = true;
        this.HP_MIX = 3000;
        this.HP = HP_MIX;
    }

    public Mob(MapleStoryClient msc,int x, int y,int speed,Direction dir){
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.dir = dir;
    }

    public Mob(MapleStoryClient msc,int x, int y,int speed,Direction dir,Action action){
        this(msc,x,y,speed,dir);
        this.action = action;
    }

    /**
     * 计数器，一直增加
     */
    public int count;
    /**
     * 计算当前画第几张图
     */
    private int imgNum;

    private int dieStep;
    @Override
    public void draw(Graphics g) {
        if (dieStep >= 6){
            this.live = false;
            dieStep = 0;
        }
        if (!live){
            msc.mobs.remove(this);
            return;
        }
        switch (dir){
            case LEFT:
                switch (action){
                    case STAND:
                        if(count++ % 6 == 0){
                            g.drawImage(imgs[imgNum++ % 6],x,y,null);
                        }else{
                            g.drawImage(imgs[imgNum % 6],x,y,null);
                        }
                        break;
                    case WALK:
                        if(count++ % 7 == 0){
                            g.drawImage(imgs[imgNum++ % 7 + 6],x,y,null);
                        }else{
                            g.drawImage(imgs[imgNum % 7 + 6],x,y,null);
                        }
                        break;
                    case DIE:
                        if(count++ % 3 == 0){
                            g.drawImage(imgs[dieStep++ % 6 + 13],x,y,null);
                        }else{
                            g.drawImage(imgs[dieStep % 6 + 13],x,y,null);
                        }
                        break;
                }
                break;
            case RIGHT:
                switch (action){
                    case STAND:
                        if(count++ % 6 == 0){
                            g.drawImage(imgs[imgNum++ % 6 + 19],x,y,null);
                        }else{
                            g.drawImage(imgs[imgNum % 6 + 19],x,y,null);
                        }
                        break;
                    case WALK:
                        if(count++ % 7 == 0){
                            g.drawImage(imgs[imgNum++ % 7 + 25],x,y,null);
                        }else{
                            g.drawImage(imgs[imgNum % 7 + 25],x,y,null);
                        }
                        break;
                    case DIE:
                        if(count++ % 3 == 0){
                            g.drawImage(imgs[dieStep++ % 6 + 32],x,y,null);
                        }else{
                            g.drawImage(imgs[dieStep % 6 + 32],x,y,null);
                        }
                        break;
                }
                break;
            default:
                break;
        }
        move();
        outOfBounds();
        if (HP < HP_MIX){
            blood.draw(g);
        }
    }

    @Override
    public void move() {
        switch (dir){
            case LEFT:
                switch (action){
                    case WALK:
                        walk(msc.bases);
                        tonch(msc.hero);
                        this.x -= speed;
                        break;
                    case STAND:
                        walk(msc.bases);
                        break;
                }
                break;
            case RIGHT:
                switch (action){
                    case WALK:
                        walk(msc.bases);
                        tonch(msc.hero);
                        this.x += speed;
                        break;
                    case STAND:
                        walk(msc.bases);
                        break;
                }
                break;
            default:
                break;
        }
    }


    private double v0 =Constant.INIT_JUMP_SPEED;
    private double vt;
    private double g = 9.8;
    private double delta_height;
    private double t = 0.7;
    /**
     * 自由落体 + 碰撞检测
     */
    public void walk(List<Base> bases){
        //掉的过程  自由落体
        vt = v0 + g * t;
        delta_height = v0 * t;
        v0 = vt;
        y += delta_height;
        //什么时候停止自由落体
        for (int i = 0; i < bases.size(); i++) {
            Base base = bases.get(i);
            if (this.getRectangle().intersects(base.getRectangle()) &&
                    this.y <= base.y - this.height + 40 +10){
                if (this.y >= base.y - this.height +10){
                    this.y = base.y - this.height +10;
                    v0 = Constant.INIT_JUMP_SPEED;
                    vt = 0.0;
                    break;
                }
            }
        }
    }
    /**
     * mob的移动界限
     */
    public void outOfBounds(){
        if (this.x <= 0){
            this.x = 0;
            this.dir = Direction.RIGHT;
        }
        if (this.x >= Constant.GAME_WIDTH - this.width){
            this.x = Constant.GAME_WIDTH - this.width;
            this.dir = Direction.LEFT;
        }
        if (this.dir == Direction.RIGHT){
            if(this.x >= 517 - this.width && this.y >= 767 - this.height + 40){
                this.x = 517 - this.width;
                this.dir = Direction.LEFT;
            }
            if (this.x >= 627 - this.width && this.y >= 698 - this.height + 40 &&
                    this.x <= 1510 - this.width){
                this.x = 627 - this.width;
            }
        }
        if (this.dir == Direction.LEFT){
            if (this.x <= 1510 && this.y >= 698 - this.height + 40 && this.x >= 627 - this.width){
                this.x = 1510;
                this.dir = Direction.RIGHT;
            }
        }
    }

    /**
     * 刷新延时
     */
    private int moveCount;

    /**
     * 当一个容器的某个怪物碰到人时掉血
     * @param hero 英雄对象
     */
    public void tonch(Hero hero){
        if (this.getRectangle().intersects(hero.getRectangle())){
            moveCount++;
            if (moveCount % 20 ==0){
                hero.HP -= 10;
                Power power = new Power(msc,hero.x,hero.y,10);
                msc.powers.add(power);
            }
            //创建出伤害值

        }
    }

    public Blood blood = new Blood();
    //最好可以使用外部类
    //内部类访问外部类相同的属性名时，使用  外部类名.this.属性名的方式
    class Blood extends AbstractMapleStoryObject{
        private Image img;
        public Blood(){
            this.img = ImageUtil.getImage("blood");
            this.width = img.getWidth(null);
        }

        @Override
        public void draw(Graphics g) {
            /*//画血条框框
            g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,10);
            //填充
            Color c = g.getColor();
            g.setColor(Color.GREEN);
            g.fillRect(Mob.this.x + 1,Mob.this.y - 15+1,Mob.this.width * Mob.this.HP / Mob.this.HP_MIX,10-1);
            g.setColor(c);*/
            int n = Mob.this.width / this.width * Mob.this.HP / Mob.this.HP_MIX;
            for (int i = 0; i < n; i++) {
                g.drawImage(img,Mob.this.x + (i * this.width),Mob.this.y - 15,null);
            }
        }
    }
}
